package com.itheima.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyDemo {
    public static void main(String[] args) {
        //创建真实对象
        final Dell dell = new Dell();
        //动态代理
        SaleComputer saleComputer=(SaleComputer) Proxy.newProxyInstance(dell.getClass().getClassLoader(), dell.getClass().getInterfaces(), new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if("sale".equals(method.getName())){
                    //增强参数
                    double money=(Double)args[0];
                    money=money*0.8;
                    System.out.println("专车接你");
                    String obj =(String) method.invoke(dell, money);
                    System.out.println("免费送货");
                    //增强返回值
                    return obj+"送鼠标垫";
                }else {
                    Object obj = method.invoke(dell, args);
                    return obj;
                }

            }
        });


        //调用方法
        String computer = saleComputer.sale(8000);
        System.out.println(computer);
    }
}
